/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financing.service.finserviceorder;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.financing.entity.finserviceorder.FinServiceOrder;
import com.jeesite.modules.financing.dao.finserviceorder.FinServiceOrderDao;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financing.entity.finserviceorder.TfinancingServiceOrderDetail;
import com.jeesite.modules.financing.dao.finserviceorder.TfinancingServiceOrderDetailDao;

/**
 * 融资服务订单Service
 * @author YY
 * @version 2021-03-16
 */

@Service
@Transactional(readOnly=true)
public class FinServiceOrderService extends CrudService<FinServiceOrderDao, FinServiceOrder> {
	
	@Autowired
	private TfinancingServiceOrderDetailDao tfinancingServiceOrderDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param finServiceOrder
	 * @return
	 */
	@Override
	public FinServiceOrder get(FinServiceOrder finServiceOrder) {
		FinServiceOrder entity = super.get(finServiceOrder);
		if (entity != null){
			TfinancingServiceOrderDetail tfinancingServiceOrderDetail = new TfinancingServiceOrderDetail(entity);
			tfinancingServiceOrderDetail.setStatus(TfinancingServiceOrderDetail.STATUS_NORMAL);
			entity.setTfinancingServiceOrderDetailList(tfinancingServiceOrderDetailDao.findList(tfinancingServiceOrderDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param finServiceOrder 查询条件

	 * @return
	 */
	@Override
	public Page<FinServiceOrder> findPage(FinServiceOrder finServiceOrder) {
		return super.findPage(finServiceOrder);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param finServiceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(FinServiceOrder finServiceOrder) {
		if(finServiceOrder.getIsNewRecord()){
			finServiceOrder.setFinancingServiceOrderCode(codeRuleService.getCode("financialrate"));
			finServiceOrder.setCreateBy(UserUtils.getUser().getLoginCode());
		}
		super.save(finServiceOrder);
		if (finServiceOrder.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("financialrate");
		}
		// 保存上传附件
		FileUploadUtils.saveFileUpload(finServiceOrder.getId(), "finServiceOrder_file");
		// 保存 FinServiceOrder子表
		for (TfinancingServiceOrderDetail tfinancingServiceOrderDetail : finServiceOrder.getTfinancingServiceOrderDetailList()){
			if (!TfinancingServiceOrderDetail.STATUS_DELETE.equals(tfinancingServiceOrderDetail.getStatus())){
				tfinancingServiceOrderDetail.setFinancingServiceOrderId(finServiceOrder);
				if (tfinancingServiceOrderDetail.getIsNewRecord()){
					tfinancingServiceOrderDetailDao.insert(tfinancingServiceOrderDetail);
				}else{
					tfinancingServiceOrderDetailDao.update(tfinancingServiceOrderDetail);
				}
			}else{
				tfinancingServiceOrderDetailDao.delete(tfinancingServiceOrderDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param finServiceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(FinServiceOrder finServiceOrder) {
		super.updateStatus(finServiceOrder);
	}
	
	/**
	 * 删除数据
	 * @param finServiceOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(FinServiceOrder finServiceOrder) {
		super.delete(finServiceOrder);
		TfinancingServiceOrderDetail tfinancingServiceOrderDetail = new TfinancingServiceOrderDetail();
		tfinancingServiceOrderDetail.setFinancingServiceOrderId(finServiceOrder);
		tfinancingServiceOrderDetailDao.deleteByEntity(tfinancingServiceOrderDetail);
	}


	
}